package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.ServiceTag;
import com.jlwl.houseservice.service.ServiceTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务标签控制器
 */
@RestController
@RequestMapping("/service-tags")
public class ServiceTagController {

    @Autowired
    private ServiceTagService serviceTagService;

    /**
     * 获取服务标签列表（分页）
     */
    @GetMapping
    public Result<Page<ServiceTag>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "status", required = false) Integer status) {
        
        LambdaQueryWrapper<ServiceTag> queryWrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            queryWrapper.eq(ServiceTag::getStatus, status);
        }
        queryWrapper.orderByAsc(ServiceTag::getId);
        
        Page<ServiceTag> pageResult = serviceTagService.page(new Page<>(page, size), queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取所有启用的服务标签
     */
    @GetMapping("/all")
    public Result<List<ServiceTag>> listAll() {
        List<ServiceTag> list = serviceTagService.list(
                new LambdaQueryWrapper<ServiceTag>()
                        .eq(ServiceTag::getStatus, 1)
                        .orderByAsc(ServiceTag::getId)
        );
        return Result.success(list);
    }

    /**
     * 获取服务标签详情
     */
    @GetMapping("/{id}")
    public Result<ServiceTag> getById(@PathVariable Integer id) {
        ServiceTag serviceTag = serviceTagService.getById(id);
        if (serviceTag == null) {
            return Result.error("服务标签不存在");
        }
        return Result.success(serviceTag);
    }

    /**
     * 添加服务标签
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<ServiceTag> add(@RequestBody ServiceTag serviceTag) {
        serviceTag.setCreatedAt(LocalDateTime.now());
        serviceTag.setUpdatedAt(LocalDateTime.now());
        serviceTagService.save(serviceTag);
        return Result.success(serviceTag);
    }

    /**
     * 更新服务标签
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> update(@PathVariable Integer id, @RequestBody ServiceTag serviceTag) {
        ServiceTag existTag = serviceTagService.getById(id);
        if (existTag == null) {
            return Result.error("服务标签不存在");
        }
        
        serviceTag.setId(id);
        serviceTag.setUpdatedAt(LocalDateTime.now());
        serviceTagService.updateById(serviceTag);
        return Result.success();
    }

    /**
     * 删除服务标签
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> delete(@PathVariable Integer id) {
        ServiceTag existTag = serviceTagService.getById(id);
        if (existTag == null) {
            return Result.error("服务标签不存在");
        }
        
        serviceTagService.removeById(id);
        return Result.success();
    }

    /**
     * 更新服务标签状态
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        ServiceTag existTag = serviceTagService.getById(id);
        if (existTag == null) {
            return Result.error("服务标签不存在");
        }
        
        ServiceTag updateTag = new ServiceTag();
        updateTag.setId(id);
        updateTag.setStatus(status);
        updateTag.setUpdatedAt(LocalDateTime.now());
        serviceTagService.updateById(updateTag);
        return Result.success();
    }
} 